package com.zxcrm.zxcrm.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.zxcrm.common.utils.DateUtils;
import com.zxcrm.common.utils.SecurityUtils;
import com.zxcrm.zxcrm.domain.ZxcrmBusinessContact;
import com.zxcrm.zxcrm.domain.ZxcrmBusinessOpportunity;
import com.zxcrm.zxcrm.mapper.ZxcrmBusinessOpportunityMapper;
import com.zxcrm.zxcrm.service.IZxcrmBusinessContactService;
import com.zxcrm.zxcrm.service.IZxcrmBusinessOpportunityService;
import com.zxcrm.zxcrm.service.IZxcrmBussinessProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商机Service业务层处理
 * 
 * @author xu
 * @date 2025-07-08
 */
@Service
public class ZxcrmBusinessOpportunityServiceImpl implements IZxcrmBusinessOpportunityService 
{
    @Autowired
    private ZxcrmBusinessOpportunityMapper zxcrmBusinessOpportunityMapper;
    @Autowired
    private IZxcrmBussinessProductService bussinessProductService;
    @Autowired
    private IZxcrmBusinessContactService bussinessContactService;

    /**
     * 查询商机
     * 
     * @param id 商机主键
     * @return 商机
     */
    @Override
    public ZxcrmBusinessOpportunity selectZxcrmBusinessOpportunityById(Long id)
    {
        return zxcrmBusinessOpportunityMapper.selectZxcrmBusinessOpportunityById(id);
    }

    /**
     * 查询商机列表
     * 
     * @param zxcrmBusinessOpportunity 商机
     * @return 商机
     */
    @Override
    public List<ZxcrmBusinessOpportunity> selectZxcrmBusinessOpportunityList(ZxcrmBusinessOpportunity zxcrmBusinessOpportunity)
    {
        return zxcrmBusinessOpportunityMapper.selectZxcrmBusinessOpportunityList(zxcrmBusinessOpportunity);
    }

    /**
     * 新增商机
     * 
     * @param zxcrmBusinessOpportunity 商机
     * @return 结果
     */
    @Override
    public int insertZxcrmBusinessOpportunity(ZxcrmBusinessOpportunity zxcrmBusinessOpportunity)
    {
        zxcrmBusinessOpportunity.setCreateTime(DateUtils.getNowDate());
        zxcrmBusinessOpportunity.setCreateBy(SecurityUtils.getUserId().toString());
        zxcrmBusinessOpportunityMapper.insertZxcrmBusinessOpportunity(zxcrmBusinessOpportunity);
        setBusinessProduct(zxcrmBusinessOpportunity);
        setBusinessContact(zxcrmBusinessOpportunity);
        return 1;
    }

    private void setBusinessContact(ZxcrmBusinessOpportunity zxcrmBusinessOpportunity)
    {
        if (ObjectUtil.isNotEmpty(zxcrmBusinessOpportunity.getContactIds()))
        {
            List<String> contactIds = StrUtil.split(zxcrmBusinessOpportunity.getContactIds(), ",");
            List<ZxcrmBusinessContact> businessContactList = contactIds.stream().map(x -> new ZxcrmBusinessContact(zxcrmBusinessOpportunity.getId(), Long.valueOf(x))).collect(Collectors.toList());
            bussinessContactService.insertBatch(businessContactList);
        }
    }

    private void setBusinessProduct(ZxcrmBusinessOpportunity zxcrmBusinessOpportunity)
    {
        if (ObjectUtil.isNotEmpty(zxcrmBusinessOpportunity.getProductList()))
        {
            zxcrmBusinessOpportunity.getProductList().stream().forEach(x->x.setBussinessOpportunityId(zxcrmBusinessOpportunity.getId()));
            bussinessProductService.insertBatch(zxcrmBusinessOpportunity.getProductList());
        }
    }

    /**
     * 修改商机
     * 
     * @param zxcrmBusinessOpportunity 商机
     * @return 结果
     */
    @Override
    public int updateZxcrmBusinessOpportunity(ZxcrmBusinessOpportunity zxcrmBusinessOpportunity)
    {
        zxcrmBusinessOpportunity.setUpdateTime(DateUtils.getNowDate());
        zxcrmBusinessOpportunity.setUpdateBy(SecurityUtils.getUserId().toString());
        updateBusinessProduct(zxcrmBusinessOpportunity);
        updateBusinessContact(zxcrmBusinessOpportunity);
        return zxcrmBusinessOpportunityMapper.updateZxcrmBusinessOpportunity(zxcrmBusinessOpportunity);
    }

    private void updateBusinessContact(ZxcrmBusinessOpportunity zxcrmBusinessOpportunity)
    {
        if (ObjectUtil.isNotEmpty(zxcrmBusinessOpportunity.getContactIds()))
        {
            bussinessContactService.deleteZxcrmBusinessContactByBussinessOpportunityId(zxcrmBusinessOpportunity.getId());
            List<String> contactIds = StrUtil.split(zxcrmBusinessOpportunity.getContactIds(), ",");
            List<ZxcrmBusinessContact> businessContactList = contactIds.stream().map(x -> new ZxcrmBusinessContact( zxcrmBusinessOpportunity.getId(), Long.valueOf(x))).collect(Collectors.toList());
            bussinessContactService.insertBatch(businessContactList);
        }
    }

    private void updateBusinessProduct(ZxcrmBusinessOpportunity zxcrmBusinessOpportunity)
    {
        if (ObjectUtil.isNotEmpty(zxcrmBusinessOpportunity.getProductList()))
        {
            bussinessProductService.deleteZxcrmBussinessProductByBussinessOpportunityId(zxcrmBusinessOpportunity.getId());
            zxcrmBusinessOpportunity.getProductList().stream().forEach(x->x.setBussinessOpportunityId(zxcrmBusinessOpportunity.getId()));
            bussinessProductService.insertBatch(zxcrmBusinessOpportunity.getProductList());
        }
    }

    /**
     * 批量删除商机
     * 
     * @param ids 需要删除的商机主键
     * @return 结果
     */
    @Override
    public int deleteZxcrmBusinessOpportunityByIds(Long[] ids)
    {
        delBusinessProduct(ids);
        delBusinessContact(ids);
        return zxcrmBusinessOpportunityMapper.deleteZxcrmBusinessOpportunityByIds(ids);
    }

    private void delBusinessContact(Long[] ids)
    {
        for (Long id : ids)
        {
            bussinessContactService.deleteZxcrmBusinessContactByBussinessOpportunityId(id);
        }
    }

    private void delBusinessProduct(Long[] ids)
    {
        for (Long id : ids)
        {
            bussinessProductService.deleteZxcrmBussinessProductByBussinessOpportunityId(id);
        }

    }

    /**
     * 删除商机信息
     * 
     * @param id 商机主键
     * @return 结果
     */
    @Override
    public int deleteZxcrmBusinessOpportunityById(Long id)
    {
        return zxcrmBusinessOpportunityMapper.deleteZxcrmBusinessOpportunityById(id);
    }


}
